#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：6.json数据保存.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-29 14:31
'''
import json

import requests
from lxml import etree

url = "https://www.4399.com/flash/"
response = requests.get(url).content.decode("gbk")
html_tree = etree.HTML(response)

a_list = html_tree.xpath('//ul[@class="n-game cf"]/li/a')
game_list = list()
for a_temp in a_list:
    item = dict()
    item['href'] = a_temp.xpath("./@href")[0]
    item['title'] = a_temp.xpath("./b/text()")[0]
    game_list.append(item)

print(game_list)

with open("6game_info.json", 'w', encoding='utf-8') as f:
    # python内置对象无法序列化，所以无法直接写入
    # f.write(game_list)
    f.write(json.dumps(game_list, indent=4, ensure_ascii=False))


